#!/usr/bin/env python

from rdflib import *
import urllib
import sys
import re

SKOS = Namespace("http://www.w3.org/2004/02/skos/core#")
APIBASE = 'http://localhost/onki-light/rest/'
PROPS = (SKOS.prefLabel, SKOS.altLabel, SKOS.hiddenLabel)

# 1st arg: vocabulary id
vocab = sys.argv[1]
# 2nd arg: base URL for REST API
if len(sys.argv) > 2:
  APIBASE = sys.argv[2]

# get data
vocdata = urllib.urlopen(APIBASE + "data/" + vocab)

# parse
g = Graph()
g.parse(vocdata)

labels = set()
for prop in PROPS:
  for conc, label in g.subject_objects(prop):
    # check it's a concept
    if (conc, RDF.type, SKOS.Concept) not in g:
      continue
    # filter simple cases
    if re.match(r'^[a-z]+$', label) is None:
      for i in range(len(label)):
        if i > 10: break
        labels.add((unicode(label)[:i+1], unicode(label.language)))

#print >>sys.stderr, repr(labels)

for prefix, lang in sorted(labels):
  print "%s\t%s" % (prefix.encode('UTF-8'), lang.encode('UTF-8'))
